/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.controller;

import com.tesam.gedeon.entity.Country;
import com.tesam.gedeon.service.CountryService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Sistema
 */
@Controller
public class CountryController {
    
    @Autowired
    SystemUserController system ;
    @Autowired
    CountryService countryService ; 
    
    @RequestMapping(value = "/mediationcountry/index")
    public ModelAndView index(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationcountry/index");
          
        system.loadDataSystem(mv, request);   
        
        request.setAttribute("totalCountry", countryService.getCountAll());
          
        mv.addObject("country", countryService.getAllByLimit(0, 20, "name", "asc")); 
         
        return mv ;
    }
    
    @RequestMapping(value = "/mediationcountry/update")
    public ModelAndView viewUpdate(@ModelAttribute("Country") Country country , 
                                    HttpServletRequest request){
    ModelAndView mv = new ModelAndView("/mediationcountry/update");
    
    int id = 0 ;
            
      if(request.getParameter("id") != null){
          id = Integer.parseInt(request.getParameter("id").toString()) ;
      }
    
    country.setId(id);
    
    mv.addObject("country", countryService.getCountryById(country));
    
    system.loadDataSystem(mv, request);
    
    return mv ;
    }
     
    @RequestMapping(value = "/mediationcountry/saveupdate")
    public ModelAndView saveUpdate(@ModelAttribute("Country")Country country, HttpServletRequest request)
    {
        ModelAndView mv = new ModelAndView("/mediationcountry/save");
        
        system.loadDataSystem(mv, request);
        
        countryService.updateCountry(country);
        
        mv.addObject("id", country.getId());
        
        return mv;
    }
            
    
    @RequestMapping(value = "/mediationcountry/save"  )
    public ModelAndView save( @ModelAttribute("Country")Country country, HttpServletRequest request ,
                                SessionStatus status ){
        ModelAndView mv = new ModelAndView("/mediationcountry/view");
        
        system.loadDataSystem(mv, request);
        
        countryService.updateCountry(country);
        
        mv.addObject("id",country.getId() );
        
        return mv;
    }
    
    @RequestMapping(value = "/mediationcountry/view")
    public ModelAndView view(@ModelAttribute("Country")Country country , 
                            HttpServletRequest request ,SessionStatus status){
        ModelAndView mv = new ModelAndView("/mediationcountry/view");
        
        system.loadDataSystem(mv, request);
        
        mv.addObject("country", countryService.getCountryById(country));
        
        return mv ;
    }
    
    @RequestMapping(value = "/mediationcountry/new")
    public ModelAndView newCountry(@ModelAttribute("Country")Country country ,
                                   HttpServletRequest request ,SessionStatus status){
        ModelAndView mv = new ModelAndView("/mediationcountry/new");
        
        system.loadDataSystem(mv, request);
        
        return mv ;
    }
    
    @RequestMapping(value = "/mediationcountry/newsave" )
    public ModelAndView savenew(@ModelAttribute("Country")Country country ,
                                HttpServletRequest request ,SessionStatus status){
        ModelAndView mv = new ModelAndView("/mediationcountry/save");
            
            system.loadDataSystem(mv, request);
            
            mv.addObject("country", countryService.saveCountry(country));
            
        return mv ; 
    }
    
    @RequestMapping(value = "/mediationcountry/delete")
    public ModelAndView delete(@ModelAttribute("Country")Country country ,
                               HttpServletRequest request ,SessionStatus status ){
        ModelAndView mv = new ModelAndView("/mediationcountry/delete");
              
           system.loadDataSystem(mv, request);     
          
           mv.addObject("country", countryService.deleteCountry(country));
                
        return mv ;
    }
    
     @RequestMapping(value = "/mediationcountry/ajax" , method = RequestMethod.GET)
    public @ResponseBody
    String doAjaxGet() {

        int cantidadRegistros = 0;
        try {
            List<Country> list = countryService.getAllByLimit(0, 20, "name", "asc");

            // ArrayList<Cliente> list = clienteService.getAllClientes();
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();

            if (list.size() > 0) {
                for (Country country : list) {
                    JSONArray arrayPut = new JSONArray(); 
                    arrayPut.put(country.getCode()); 
                    arrayPut.put(country.getLongCode()); 
                    arrayPut.put(country.getName()); 
                    array.put(arrayPut);
                    // cantidadRegistros++;
                }
            }


            cantidadRegistros = countryService.getCountAll();
            json.put("iTotalRecords", cantidadRegistros);
            json.put("iTotalDisplayRecords", cantidadRegistros);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.print("Error - Controller-doAjax:" + e.getMessage());
        }

        return null;
    }
     
      @RequestMapping(value = "/mediationcountry/ajax", method = RequestMethod.POST)
    public @ResponseBody
    String doAjaxPost(HttpServletRequest request) {

        List<Country> listCountry = null;
        int cantidadRegistros = 0;

        String[] columns = {"code", "longCode", "Name" , "Name" };

        String startLimit = request.getParameter("iDisplayStart");
        String endLimit = request.getParameter("iDisplayLength");
        String dataToSort = columns[Integer.valueOf(request
                .getParameter("iSortCol_0"))];
        String directionOfOrder = request.getParameter("sSortDir_0");
        String textSearch = request.getParameter("sSearch").toUpperCase();

        int _startLimit = 0;
        int _endLimit = 0;

        if (startLimit != null && !startLimit.equals("0")) {

            if (!startLimit.trim().isEmpty()) {
                _startLimit = Integer.valueOf(startLimit);
            }
        }

        if (endLimit != null && !endLimit.equals("0")) {
            if (!endLimit.trim().isEmpty()) {
                _endLimit = Integer.valueOf(endLimit);
            }
        }

        if (textSearch != null) {
            if (!textSearch.trim().isEmpty()) {
                textSearch = textSearch.trim();
                Country country = new Country();
                country.setName(textSearch);
                listCountry = countryService.getSearchCountryByLimit(_startLimit,_endLimit,"name" , "asc" , country);
                cantidadRegistros = countryService.getTotalBySearch(country);
                return countryService.getJsonDataFromArrayList(listCountry, cantidadRegistros);

            }
        }

        listCountry = countryService.getAllByLimit(_startLimit,
                (_endLimit), dataToSort, directionOfOrder);

        cantidadRegistros = countryService.getCountAll();

        return countryService.getJsonDataFromArrayList(listCountry, cantidadRegistros);
    }


    
}

